<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
  <style>
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }

    .calculator {
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 10px;
      background-color: #f9f9f9;
    }
	
    input[type="text"] {
      width: 210px;
      height: 30px;
      margin-bottom: 10px;
      text-align: right;
    }

    button {
      width: 60px;
      height: 40px;
      margin: 5px;
    }
  </style>
</head>

<body>
  <div class="calculator">
		<input type="text" id="display" readonly>
		<button id="backspace">←</button>
    <div>
		<button id="clear">C</button>
		<button id="left-paren">(</button>
		<button id="right-paren">)</button>
		<button id="percent">%</button>
    </div>
    <div>
      <button>7</button>
      <button>8</button>
      <button>9</button>
      <button id="add">+</button>
    </div>
    <div>
      <button>4</button>
      <button>5</button>
      <button>6</button>
      <button id="subtract">-</button>
    </div>
    <div>
      <button>1</button>
      <button>2</button>
      <button>3</button>
      <button id="multiply">*</button>
    </div>
    <div>
      <button>0</button>
      <button>.</button>
      <button id="equals">=</button>
      <button id="divide">/</button>
    </div>
  </div>

  <script>
		let display = document.getElementById('display');
		let calculator = document.querySelector('.calculator');
		calculator.addEventListener('click', function (event) {
		if (event.target.tagName == 'BUTTON') {
			let buttonText = event.target.textContent;
        switch (buttonText) {
          case 'C':
            display.value = '';
            break;
          case '←':
            display.value = display.value.slice(0, -1);
            break;
          case '=':
            try {
              let result = eval(display.value);
              if (isNaN(result) ||!isFinite(result)) {
                display.value = 'ERROR';
              } else {
                display.value = parseFloat(result.toFixed(15));
              }
            } catch (error) {
              display.value = 'ERROR';
            }
            break;
          default:
            display.value += buttonText;
        }
      }
    });
  </script>
</body>

</html>